// http.ts
import { showToast } from 'vant';
import axios from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

class HttpService {
  private axiosInstance: AxiosInstance;

  constructor(baseURL: string, config?: AxiosRequestConfig) {
    this.axiosInstance = axios.create({
      baseURL,
      ...config,
    });

    // 添加请求拦截器
    this.axiosInstance.interceptors.request.use(
      (request) => {
        // 在发送请求之前做些什么，比如添加认证token
        showToast({
          iconSize: '10em',
          duration: 1500,
          icon: 'https://mall.leyifan.com/static/h5/images/loading.gif',
          style: {
            background: 'transparent',
            transform:'scale(2)'
            
          },
          overlay: true,
          overlayStyle:{
            show:true,
            customStyle: {
              backgroundColor: 'rgb(255, 255, 255) !important',
              backdropFilter: 'blur(25px)',
              width: '100vw',
              height: '100vh'
            }
          }
        });
        return request;

      },
      (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
      }
    );

    // 添加响应拦截器
    this.axiosInstance.interceptors.response.use(
      (response: AxiosResponse) => {
        // 对响应数据做点什么，比如统一处理错误
        return response;
      },
      (error) => {
        // 对响应错误做点什么
        return Promise.reject(error);
      }
    );
  }

  get<T>(url: string, params?: any, config?: AxiosRequestConfig): Promise<T> {
    return this.axiosInstance.get<T>(url, { params, ...config });
  }

  post<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
    return this.axiosInstance.post<T>(url, data, { ...config });
  }

  // 你可以继续添加其他HTTP方法，比如put, delete等
}

// 创建一个HTTP服务的实例，并导出它
const _axios = new HttpService('https://api.mall.leyifan.com',{
  headers:{
    Appplatform:'other',
    // Host:'<calculated when request is sent>',
    'Content-Type':'application/json',
    // 'Authori-zation':'78b960ae14e2499d8c12bfccff37f8fb',
    // 'Content-Length':'<calculated when request is sent>',

    // Host:'<calculated when request is sent>',
  }
});
export default _axios;







